package com.hylanda.config;

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/** 
 * @author zhangy
 * @E-mail:blackoon88@gmail.com 
 * @qq:846579287
 * @version created at：2017年11月8日 下午5:56:26 
 * 证书生成 keytool -genkey -alias tomcat -keyalg RSA
 */
@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter{
	/**
	 * 配置Https
	 * @return
	 */
	@Bean
	public EmbeddedServletContainerCustomizer containerCustomizer() {
	    return new EmbeddedServletContainerCustomizer() {
	        @Override
	        public void customize(ConfigurableEmbeddedServletContainer container) {
	            ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
	            ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
	            ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");
	            container.addErrorPages(error401Page, error404Page, error500Page);
	        }
	    };
	}
	/**
	 * 配置Http使其自动重定向到Https
	 * @param properties
	 * @return
	 */
//	@Bean
//	public EmbeddedServletContainerFactory servletContainer() {
//	    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(){
//	    	 @Override
//	            protected void postProcessContext(Context context) {
//	                //SecurityConstraint必须存在，可以通过其为不同的URL设置不同的重定向策略。
//	                SecurityConstraint securityConstraint = new SecurityConstraint();
//	                securityConstraint.setUserConstraint("CONFIDENTIAL");
//	                SecurityCollection collection = new SecurityCollection();
//	                collection.addPattern("/*");
//	                securityConstraint.addCollection(collection);
//	                context.addConstraint(securityConstraint);
//	            }
//	    };
//	    tomcat.addAdditionalTomcatConnectors(createSslConnector());
//	    return tomcat;
//	}
//	public Connector createSslConnector() {
//	    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
//	    connector.setScheme("http");
//	    connector.setSecure(false);
//	    connector.setPort(8080);
//	    connector.setRedirectPort(8443);
//	    return connector;
//	}

}
